﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;

namespace LuShanTea.Service
{
    public class GoodAttrService :IGoodAttrService
    {

       
        // 对指定商品添加属性
        public long AddNew(GoodAttrDTO goodAttr)
        {
            
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodAttr> bs = new BaseService<GoodAttr>(ctx);
                bool exit = bs.GetAll().Any(g => g.ProNumber == goodAttr.ProNumber);
                if (exit)
                {
                    throw new ArgumentException($"商品id为：{goodAttr.GoodId}的商品已存在属性");
                }
                GoodAttr goodAtt = new GoodAttr();
                goodAtt.ArtDesc = goodAtt.ArtDesc;
                goodAtt.ProduceTime = goodAttr.ProduceTime;
                goodAtt.ProNumber = goodAttr.ProNumber;
                goodAtt.ValidTime = goodAttr.ValidTime;
                goodAtt.Level = goodAttr.Level;
                goodAtt.GoodId = goodAttr.GoodId;
                ctx.GoodAttrs.Add(goodAtt);
                ctx.SaveChanges();
                return goodAtt.Id;
            }
        }

        // 获取指定商品id的属性
        public GoodAttrDTO GetAttrByGoodId(long goodId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodAttr> bs = new BaseService<GoodAttr>(ctx);
                var data = bs.GetAll().Where(g => g.GoodId == goodId).SingleOrDefault();
                if (data == null)
                {
                    //throw new ArgumentException($"不存在商品id为{goodId}的属性");
                    return null;
                }
                return ToDTO(data);
            }
        }

        // 获取属性列中所有的属性
        public GoodAttrDTO[] GetAttrPageData()
        {

            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodAttr> bs = new BaseService<GoodAttr>(ctx);
                return bs.GetAll().Include(g => g.Good).AsNoTracking().ToList().Select(g => ToDTO(g)).ToArray();
            }
        }

        public void MakeDelete(long goodAttrId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodAttr> bs = new BaseService<GoodAttr>(ctx);
                bs.MakeDelete(goodAttrId);
            }
        }

        // 更新属性表中的属性值
        public void UpdataGoodAttr(GoodAttrDTO goodAttr)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodAttr> bs = new BaseService<GoodAttr>(ctx);
                var gattr = bs.GetById(goodAttr.Id);
                if(gattr == null)
                {
                    throw new ArgumentException($"商品id为：{goodAttr.GoodId}的商品不存在");
                }

                
                gattr.ArtDesc = goodAttr.ArtDesc;
                gattr.CreateDateTime = goodAttr.CreateDateTime;
                gattr.GoodId = goodAttr.GoodId;
                gattr.Id = goodAttr.Id;
                gattr.Level = goodAttr.Level;
                gattr.ProduceTime = goodAttr.ProduceTime;
                gattr.ProNumber = goodAttr.ProNumber;
                gattr.ValidTime = goodAttr.ValidTime;

                
                ctx.SaveChanges();
            }
        }

        public GoodAttrDTO ToDTO(GoodAttr gattr)
        {
            GoodAttrDTO dto = new GoodAttrDTO();
            dto.GoodName = gattr.Good.Name;
            dto.ArtDesc = gattr.ArtDesc;
            dto.CreateDateTime = gattr.CreateDateTime;
            dto.GoodId = gattr.GoodId;
            dto.Id = gattr.Id;
            dto.Level = gattr.Level;
            dto.ProduceTime = gattr.ProduceTime;
            dto.ProNumber = gattr.ProNumber;
            dto.ValidTime = gattr.ValidTime;
            return dto;
        }
        // 获取所有的商品属性
        public GoodAttrDTO[] GetAll()
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodAttr> bs = new BaseService<GoodAttr>(ctx);
                return bs.GetAll().ToList().Select(g=>ToDTO(g)).ToArray();
            }
        }
        // 分页获取商品属性
        public GoodAttrDTO[] GetPageData(int index)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodAttr> bs = new BaseService<GoodAttr>(ctx);
                return bs.GetPageData(index,10).ToList().Select(g => ToDTO(g)).ToArray();
            }
        }

        public GoodAttrDTO GetGoodAtrById(long goodAttrId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<GoodAttr> bs = new BaseService<GoodAttr>(ctx);
                var data = bs.GetAll().Where(g => g.Id == goodAttrId).SingleOrDefault();
                if (data == null)
                {
                    throw new ArgumentException($"不存在商品id为{goodAttrId}的属性");
                }
                return ToDTO(data);
            }
        }
    }
}
